package com.hyfd.rabbitMq;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import com.hyfd.common.utils.ExceptionUtils;
import com.hyfd.common.utils.MapUtils;
import com.hyfd.dao.mp.BoluomiProductDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.deal.BaseDeal;
import com.hyfd.deal.Flow.QianMiCountryFlowDeal;

public class ChannelMqListener implements MessageListener{
	
	Logger log = Logger.getLogger(getClass());
	
	@Autowired
	RabbitMqProducer mqProducer;
	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//分销商物理通道
	
	@Autowired
	BoluomiProductDao boluomiProductDao;
	
	@Override
	public synchronized void onMessage(Message message) {
		Map<String, Object> order = SerializeUtil.getObjMapFromMessage(message);//获取订单对象
		try {
			String providerMark = (String) order.get("providerMark");//获取通道的唯一标识
			Map<String,Object> result = new HashMap<String,Object>();
			String bizType = (String) order.get("bizType");
			String pkg = bizType.equals("1")?"Flow":"Bill";
			BaseDeal deal = (BaseDeal) Class.forName("com.hyfd.deal."+pkg+"."+providerMark).newInstance();
			result = deal.deal(order);
			result.put("order", order);
			mqProducer.sendDataToQueue(RabbitMqProducer.Status_QueueKey, SerializeUtil.getStrFromObj(result));
		} catch (Exception e) {
			log.error("通道选择出错"+ExceptionUtils.getExceptionMessage(e)+"||"+MapUtils.toString(order));
			e.printStackTrace();
		} 
	}
	
}
